<?php
class Io
{
    protected $path;
    protected $packageCode;

    public function __construct($fileTruename)
    {
        $this->path = $fileTruename;
        $this->packageCode = 'abcdefd';
    }

    public function createFile($fileName)
    {
        $package = $this->packageCode. "\r\n";
        $file = $this->openFile($fileName, 'w');

        for ($i = 0; $i < 50000; $i++) {
            fwrite($file, $package);
        }

        fclose($file);
    }

    public function cutFileOne($originalFileName, $newFileName, $cutLines)
    {
        if ( is_file($this->path. $originalFileName) && $cutLines ) {
            $contents = file($this->path. $originalFileName);
            $countLines = count($contents);

            if ( $countLines >= $cutLines ) {
                $output = array_slice($contents, 0, $cutLines);
                $newContents = array_slice($contents, $cutLines);
                file_put_contents($this->path. $originalFileName, $newContents);
                return str_replace(["\r\n","\n","\r"],'',$output[0]);
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }

    public function cutFileMulti($originalFileName, $newFileName, $cutLines)
    {
        if ( is_file($this->path. $originalFileName) && $cutLines) {
            $contents = file($this->path. $originalFileName);
            $countLines = count($contents);

            if ( $countLines >= $cutLines ) {
                $output = array_slice($contents, 0, $cutLines);
                $newContents = array_slice($contents, $cutLines);

                $rs = file_put_contents($this->path. $originalFileName, $newContents);

                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='. $newFileName);

                foreach ($output as $value) {
                    echo $value;
                }

                return [
                    'status' => true
                ];
            }
            else {
                return [
                    'status' => false,
                    'message' => '礼包码不足 ...'
                ];
            }
        }
        else {
            return [
                'status' => false,
                'message' => '查找不到文件 ...'
            ];
        }
    }


    public function addToFile($originalFileName, $newFileName)
    {
        $originalFile = $this->path. $originalFileName;
        $newFile = $newFileName;

        if ( is_file($originalFile) && is_file($newFile) ) {
            $originalArr = file($originalFile);
            $newArr = file($newFile);
            $endStr = $originalArr[count($originalArr)-1];

            if( ! strstr($endStr, "\n") ){
                $originalArr[count($originalArr)-1] = $endStr . "\n";
            }
            $contents = array_merge($originalArr, $newArr);

            file_put_contents($originalFile, $contents);

            return [
                'status' => true
            ];
        }
        else {
            return false;
        }
    }


    public function fileCountLines($fileName)
    {
        return count(file($fileName));
    }

    protected function openFile($fileName, $operation)
    {
        $filePath = $this->path. $fileName;
        $fileInfo = pathinfo($filePath);

        if ($fileInfo['extension'] == 'txt') {
            return fopen($this->path. $fileName, $operation);
        }
    }

    protected function getFileLines($file)
    {
        $line = 0;

        if ($file) {
            while(stream_get_line($file, 1024, "\n")) {
                $line++;
            }
        }

        return $line;
    }

    protected function closeFile($file)
    {
        if ($file) {
            fclose($file);
        }
    }
}
